/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actor.enemies;

import Actor.Actor;
import Actor.Enemy;
import Actor.Projectile;
import actor.weapons.Explosion;
import actor.weapons.RPG;
import actor.weapons.Weapon;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import javax.vecmath.Point2d;
import projectshanghai.World;

/**
 *
 * @author Eric-Standard
 */
public class Boss extends Enemy {

    private boolean noticedPlayer = false;
    private boolean shotByPlayer = false;
    private Weapon weapon;
    private int weaponCooldown = 100;
    private static final int WEAPON_COOLDOWN = 500;
    private int health;
    private int healthBarTime = 0;
    private boolean healthBarShown = false;
    private static final int HEALTH_BAR_TIMEOUT = 7000;
    public static final int STARTING_HEALTH = 200;
    private Rectangle r = new Rectangle();
    private Area area;

    public Boss() {
        super();
        this.health = STARTING_HEALTH;
        r.width = 20;
        r.height = 20;
    }

    @Override
    public void act(long dt) {
        weapon.act(dt);
        if (health <= 0) {
            this.isActive = false;
            return;
        }
        if (healthBarShown) {
            healthBarTime -= dt;
            if (healthBarTime < 0) {
                healthBarShown = false;
            }
        }
        super.act(dt);
        //Get player info
        Point2d posP = world.getPlayer().getPos();
        double angleToPlayer = Math.atan2(posP.y - pos.y, posP.x - pos.x);
        double distance = posP.distance(pos);
        if (shotByPlayer) {
            noticedPlayer = true;
        }
        if (!noticedPlayer) {
            //Check both distance and angle
            if (distance < 500 && Math.abs(angle - angleToPlayer) < Math.PI / 6) {
                noticedPlayer = true;
            }
        } else if (distance > 500 + (shotByPlayer ? 150 : 0)) {
            //Reset flags and velocity
            noticedPlayer = false;
            shotByPlayer = false;
            vel.x = vel.y = 0;
        }
        if (noticedPlayer) {
            angle = angleToPlayer + (Math.random() - .5) * .5;
            if (distance > 100) {
                vel.set(posP.x - pos.x, posP.y - pos.y);
                vel.normalize();
                vel.scale(100);
            } else {
                vel.x = vel.y = 0;
            }
            //Weapon firing
            if (weaponCooldown > 0) {
                weaponCooldown -= dt;
            }
            if (distance < 500 && weaponCooldown <= 0) {
                weapon.fire();
                weaponCooldown = WEAPON_COOLDOWN;
            }
        }
        r.x = (int) pos.x;
        r.y = (int) pos.y;
        area = new Area(r);
    }

    @Override
    public void putSelfInWorld(World world) {
        super.putSelfInWorld(world);
        weapon = new RPG(this);
        weapon.addAmmo(1000);
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect((int) pos.x, (int) pos.y, 20, 20);
        weapon.draw(g);
        if (healthBarShown && health < STARTING_HEALTH) {
            g.setColor(Color.BLACK);
            g.drawRect((int) pos.x, (int) pos.y + 25, 20, 5);
            g.setColor(Color.RED);
            g.fillRect((int) pos.x, (int) pos.y + 25, (int) (20.0 * health / STARTING_HEALTH), 5);
        }
    }

    @Override
    public void collide(Actor a) {
        //System.out.println(health);
        if (a instanceof Explosion) {
            Explosion p = (Explosion) a;
            this.health -= p.getDamage(this);
            if (health <= 0) {
                this.isActive = false;
                this.collisionDetection = false;
            }
            healthBarShown = true;
            healthBarTime = HEALTH_BAR_TIMEOUT;
        } else if (a instanceof Projectile) {
            Projectile p = (Projectile) a;
            this.health -= p.getDamage();
            healthBarShown = true;
            healthBarTime = HEALTH_BAR_TIMEOUT;
            shotByPlayer = true;
        }
    }

    @Override
    public Rectangle2D getBoundingRectangle() {
        return r;
    }

    @Override
    public Area getArea() {
        return area;
    }
}
